<?php
namespace Home\Controller;
use Think\Controller;
class JiazhangController extends CommonController {
	//家长中心  我的预约
    public function index()
    {
        //公共显示变量
        $this->public_xianshi();

        //我的预约显示
        $sql = "select t1.*,t2.name,t2.phone from jz_to_ls_yuyue t1 inner join laoshi_user t2 on t1.ls_uid=t2.id where t1.jz_uid='{$_SESSION['id']}' and is_shenhe=0";
        $yuyues = M()->query($sql);
        $this->assign('yuyues',$yuyues);


       $this->display();
    }
    //家长预定单 取消 删除
    public function jz_order_del()
    {
        if (IS_POST) {
            $id = I('post.id');
            $res = M('jz_to_ls_yuyue')->delete($id);
            if ($res) {
                 $arr = array(
                        'error' =>1,
                        'info'=>"取消成功"
                        );
                echo json_encode($arr);
            }else {
                 $arr = array(
                        'error' =>0,
                        'info'=>"取消失败"
                        );
                echo json_encode($arr);
            }
        }
    }

    //家长中心  我的订单
    public function jz_order()
    {
        //公共显示变量
        $this->public_xianshi();

        //我的订单显示
        $sql = "select t1.*,t2.name,t2.phone from jz_to_ls_yuyue t1 inner join laoshi_user t2 on t1.ls_uid=t2.id where t1.jz_uid='{$_SESSION['id']}' and is_laoshi_on=1 and is_shenhe=1";
        $orders = M()->query($sql);
        $this->assign('orders',$orders);

       $this->display();
    }

    //家长中心 家长信息设置
    public function jiazhang_one()
    {
        //公共显示变量
        $this->public_xianshi();

        $id = $_SESSION['id'];
        $jiazhang_info = M('jiazhang_user')->where(array('id'=>$id))->find();
        $this->assign('jiazhang_info',$jiazhang_info);

        $cityid = M('city')->where(array('city_name'=>$jiazhang_info['city']))->getField('cityid');

        //显示城市
        $citys = M('city')->where('parent_id=0')->select();
        $this->assign('citys',$citys);

        $this->display();
    }
    //家长中心 家长添加 更新
    public function jiazhang_one_add()
    {   //[city] => 广州[name] => 李先生[sex] => 男
        if (IS_POST) {
            $data['city'] = I('post.city');
            $data['name'] = I('post.name');
            $data['sex'] = I('post.sex');

            $res = M('jiazhang_user')->where(array('id'=>$_SESSION['id']))->save($data);
            if ($res) {
                 $arr = array(
                        'error' =>1,
                        'info'=>"更新成功"
                        );
                echo json_encode($arr);
            }else {
                 $arr = array(
                        'error' =>0,
                        'info'=>"更新失败"
                        );
                echo json_encode($arr);
            }
        }

    }


    //家长中心 家长需求 学生表设置
    public function jiazhang_info()
    {
        //公共显示变量
        $this->public_xianshi();

    	$id = $_SESSION['id'];
    	$city_name = M('jiazhang_user')->where(array('id'=>$id))->getField('city');
        $this->assign('city_name',$city_name);

    	$cityid = M('city')->where(array('city_name'=>$city_name))->getField('cityid');

    	//对应区域
    	$city_quyu = M('city')->where(array('parent_id'=>$cityid))->select();
    	$this->assign('city_quyu',$city_quyu);

    	//授课年级显示
    	$grade_info = M('grade')->where(array('parent_id'=>0))->select();
    	$this->assign('grade_info',$grade_info);

        //科目显示
        $kecheng = M('subjects')->select();
        $this->assign('kecheng',$kecheng);

        //基本信息显示
        $jiazhang_info = M('jiazhang_xuqiu')->where(array('uid'=>$_SESSION['id']))->find();
        $this->assign('jiazhang_info',$jiazhang_info);

        //(1,2,3)转数组
        $subjects = explode(',', $jiazhang_info['subjects']);
        $this->assign('subjects',$subjects);


    	$this->display();
    }

    //家长中心 家长需求基本信息
    public function jiazhang_info_add()
    {   //Array ( [sk_city] => 广州 [quyu] => 天河区 [name] => v [sex] => 男 [grade] => 高三 [kecheng] => Array ( [0] => 1 [1] => 2 ) )
    	if (IS_POST) {
    		$data['sk_city'] = I('post.sk_city');
    		$data['sk_quyu'] = I('post.quyu');
    		$data['name'] = I('post.name');
    		$data['sex'] = I('post.sex');
    		$data['grade'] = I('post.grade');

    		$kecheng = I('post.kecheng');
    		$data['subjects'] = implode(',', $kecheng);

    		$data['uid'] = $_SESSION['id'];
    		$uid = M('jiazhang_xuqiu')->where(array('uid'=>$_SESSION['id']))->getField('uid');
    		if ($uid) {
    			$res = M('jiazhang_xuqiu')->where(array('uid'=>$_SESSION['id']))->save($data);
    			if ($res) {
			         $arr = array(
			                'error' =>1,
			                'info'=>"更新成功"
			                );
			        echo json_encode($arr);
    			}else {
			         $arr = array(
			                'error' =>0,
			                'info'=>"更新失败"
			                );
			        echo json_encode($arr);
    			}
    		}else {
    			$res = M('jiazhang_xuqiu')->add($data);
    			if ($res) {
			         $arr = array(
			                'error' =>1,
			                'info'=>"添加成功"
			                );
			        echo json_encode($arr);
    			}else {
			         $arr = array(
			                'error' =>0,
			                'info'=>"添加失败"
			                );
			        echo json_encode($arr);
    			}
    		}
    	}
    }

    //头像
    public function head()
    {
        //公共显示变量
        $this->public_xianshi();


        $id = $_SESSION['id'];
        $head = M('jiazhang_user')->where(array('id'=>$_SESSION['id']))->getField('face');
        $this->assign('head',$head);

        $this->display();
    }
    //添加 修改
    public function head_add()
    {
        if(!empty($_FILES['file']['tmp_name'])){
            $upload = new \Think\Upload();// 实例化上传类    
            $upload->maxSize   =     3145728 ;// 设置附件上传大小    
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
            $upload->savePath  =      'Uploads_jz/'; // 设置附件上传目录    
            // 上传单个文件     
            $info   =   $upload->uploadOne($_FILES['file']);    
            if(!$info) {// 上传错误提示错误信息        
                //$this->error($upload->getError());
            }else{
            // 上传成功 获取上传文件信息         
                $face =  '/Uploads'.'/'.$info['savepath'].$info['savename'];   
                M('jiazhang_user')->where(array('id'=>$_SESSION['id']))->setField('face',$face);
            }
        }
        $this->redirect('jiazhang/head');
    }

    //安全 密码
    public function password()
    {
        //公共显示变量
        $this->public_xianshi();


        $this->display();
    }

    //安全 密码重置
    public function password_add()
    {
        //Array ( [password] => qq [newpassword] => qq [renewpassword] => qq )
        if (IS_POST) {
            $password = I('post.password');
            $newpassword = I('post.newpassword');
            $renewpassword = I('post.renewpassword');

            //验证原始密码
            $password_yz = M('jiazhang_user')->where(array('id'=>$_SESSION['id']))->getField('password');
            if (md5($password) != $password_yz) {
                $arr = array(
                    'error' =>0,
                    'info' =>'原密码输入有误'
                    );
                echo json_encode($arr);
                exit;
            }

            //新密码 验证
            $preg_password = "/^[0-9A-z]{6,16}$/";
            $password_Yz = preg_match($preg_password, $newpassword);
            if (!$password_Yz) {
                $arr = array(
                    'error' => 0,
                    'info' => '密码必须是6-16位的字符串'
                    );
                echo json_encode($arr);
                exit;
            }

            //第二次密码跟第一次检测
            if ($newpassword != $renewpassword) {
                $arr = array(
                    'error' => 0,
                    'info' => '两次密码输入不一样'
                    );
                echo json_encode($arr);
                exit;
            }

            //更新
            $res = M('jiazhang_user')->where(array('id'=>$_SESSION['id']))->setField('password',md5($newpassword));
            if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '更新成功,需要重新登陆'    
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '更新失败'    
                    );
                echo json_encode($arr);
            }
        }
    }



    //无注册家长需求表 提交
    public function zhao_laoshi()
    {
        //公共显示变量
        $this->public_xianshi();

        //年级
        $grades = M('grade')->where(array('parent_id'=>0))->select();
        $this->assign('grades',$grades);
        //科目
        $kemus = M('subjects')->select();
        $this->assign('kemus',$kemus);

        $grade = I('get.grade')?I('get.grade'):'';
        $this->assign('grade',$grade);
        $kemu = I('get.kemu')?I('get.kemu'):'';
        $this->assign('kemu',$kemu);
        
    	$this->display();
    }
    //无注册家长需求表 添加
    public function zhao_laoshi_add()
    {   //[name] => 11[grade] => 11[kemu] => 11[addr] => 1[phone] => 1[beizhu] => 1[city] => 广州
    	if (IS_POST) {
    		$data['name'] = I('post.name');

            $data['grade'] = I('post.grade');
            $data['beizhu'] = I('post.beizhu');

    		$data['kemu'] = I('post.kemu');
    		$data['addr'] = I('post.addr');
    		$data['phone'] = I('post.phone');

    		$data['city'] = I('post.city');
            $data['create_time'] = time();

	       	 //手机
	       	 $preg_js = "/^1[3458][0-9]{9}$/";
	       	 $pass_sj = preg_match($preg_js, $data['phone']);
			 if (!$pass_sj) {
				$arr = array(
					'error' => 0,
					'info' => '请输入正确的手机号'
					);
				echo json_encode($arr);
				exit;
			 }

			 //入库
			 $res = M('zhao_laoshi')->add($data);
			 if ($res) {
				$arr = array(
					'error' => 1,
					'info' => '需求提交成功,我们课程顾问会第一时间联系您'
					);
				echo json_encode($arr);
			 }else {
				$arr = array(
					'error' => 0,
					'info' => '需求提交失败,请重新提交'
					);
				echo json_encode($arr);
			 }
    	}
    }

}